#include "StdAfx.h"

const float Math::sqrt2 = sqrtf(2.0f);
const float Math::PI = 3.14159265f;

double Math::SqrtTable[1000000];
double Math::QuadTable[1000000];

void Math::Init()
{
	for(ui32 i = 0; i < 1000000; ++i)
	{
		SqrtTable[i] = sqrt((double)i);
		QuadTable[i] = pow((double)i, 2.0);
	}
}

std::string WoWFolder::GetWoWPath()
{
	ConfigFile& cfg = ConfigFile::Main();
	if(cfg.ContainsKey("WoWFolder"))
	{
		std::string wowPath = cfg.GetValue("WoWFolder");
		if(GetFileAttributes(wowPath.c_str()) != INVALID_FILE_ATTRIBUTES)
			return wowPath;
	}

	HKEY key;
	LSTATUS res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Blizzard Entertainment\\World of Warcraft", 0, KEY_QUERY_VALUE, &key);
	if(res != ERROR_SUCCESS)
		return "";

	char gamepath[MAX_PATH];
	DWORD len = MAX_PATH;
	res = RegQueryValueEx(key, "InstallPath", NULL, NULL, (LPBYTE)gamepath, &len);
	if(res != ERROR_SUCCESS)
		return "";

	return gamepath;
}

std::string WoWFolder::GetLocale()
{
	static std::string knownLocales[] = 
	{
		"deDE",
		"enUS",
		"enGB",
		"ruRU",
		"koKR",
		"esMX",
		"esES",
		"frFR",
		"plPL",
		"",
	};

	std::string wowPath = GetWoWPath();
	ConfigFile& cfg = ConfigFile::Main();
	if(cfg.ContainsKey("Locale"))
	{
		std::string loc = cfg.GetValue("Locale");
		std::string data = wowPath + "\\data\\" + loc;
		if(GetFileAttributes(data.c_str()) != INVALID_FILE_ATTRIBUTES)
			return loc;
	}
		
	ui32 i = 0;
	while(knownLocales[i] != "")
	{
		std::string data = wowPath + "\\data\\" + knownLocales[i++];
		if(GetFileAttributes(data.c_str()) != INVALID_FILE_ATTRIBUTES)
			return knownLocales[i - 1];
	}

	return "";
}

bool WoWFolder::GetWoWVersion(ui32& buildOut)
{
	std::string lib = GetWoWPath() + "\\WoW.exe";
	HMODULE mod = LoadLibraryEx(lib.c_str(), NULL, LOAD_LIBRARY_AS_DATAFILE);
	if(mod == NULL)
		return false;

	ScopeHandle sc(mod, FreeLibrary);
	HRSRC res = FindResource(mod, "BLIZZARDCOMPONENT", "BLIZZARDCOMPONENT");
	if(res == NULL)
		return false;

	HGLOBAL glob = LoadResource(mod, res);
	if(glob == NULL)
		return false;

	DWORD size = 0;
	if((size = SizeofResource(mod, res)) == 0)
		return false;

	// For compatibility with old versions only:
	LPVOID data = LockResource(glob);

	std::vector<char> resData(size);
	memcpy(&resData[0], data, size);
	std::string manif = &resData[0];

	std::string::size_type pos = manif.find("<version>");
	std::string::size_type end = manif.find("</version>");

	if(pos == std::string::npos || end == std::string::npos)
		return false;

	std::stringstream strm;
	strm.str(manif.substr(pos + 9, (end - pos - 9)));
	ui32 build;
	if(!(strm >> build))
		return false;

	buildOut = build;
	return true;
}

std::string helpTerrain = 
	"You have clicked on the terrain in the world.\n"\
	"This allows you to interact with the terrain and\n"\
	"change its shape or height. Some keys are\n"\
	"available to perform these actions:\n"\
	" \n"
	"R: This key raises the terrain within the circle\n"\
	"by a certain amount you specify.\n"\
	" \n"\
	"L: This key lowers the terrain within the circle\n"\
	"by a certain amount you specify.\n"\
	" \n"\
	"I/i: These keys decrease and increase the radius\n"\
	"within which the changes are applied.\n"\
	" \n"\
	"V/v: These keys decrease and increase the\n"\
	"intensity with which the changes are made.\n";

std::string helpPanel = 
	"You are hovering a text on a panel. This text\n"\
	"shows an value of a setting in the program.\n"\
	" \n"\
	"Most of these settings can be changed either\n"\
	"with a key on the keyboard or with a special\n"\
	"control in the application. If you click on the\n"\
	"text in the panel you will see a text in the box\n"\
	"in the bottom left corner of the window with\n"\
	"information how you can change the setting.";

std::string helpTexSel = 
	"You have entered the texture selection\n"\
	"interface. Here you can search for textures\n"\
	"and select them for further usage in the\n"\
	"texturing interface.\n"\
	" \n"\
	"Enter a zone name (for example: Duskwood,\n"\
	"Durotar, Ashenvale, ...) and press load to\n"\
	"find the textures of the zone.\n"\
	" \n"
	"If you have textures in the window you can\n"\
	"select and unselect them. By pressing Add to\n"\
	"favourites the textures get placed to the\n"\
	"texturing interface.";